package com.samp.solr.solrlstm;

import com.samp.util.FilesUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 按阈值取数，满足阈值的，得出准确率。
 * 不满足阈值的，生成文件去跑模型。
 * 模型文件格式：语料\t返回的solr意图1;返回的solr意图2\t正确意图
 * 其中，返回的意图，以;隔开。
 */
public class ModeTestDataGenerate {

    private static void generateModelTestFile(String rawPath, String collectionName, String customPath, String stopWodsPath,
                                              String synonymPath, String weightPath,String outFile, String satisfyButErrorFile) throws Exception{
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        try {
            String line;
            String solrUrl = SolrlstmUtils.BASE_SOLR_URL+collectionName;
            SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
            Integer satisfyThresholdTotalNum = 0;
            Integer satisfyThresholdSuccessNum = 0;
            List<String> modelList = new ArrayList<>();
            Integer index = 0;
//            Integer count = 0;
            long allStart = System.currentTimeMillis();
            long start = System.currentTimeMillis();
            Integer notSatisfyNum = 0; //不满足阈值的数量
            Integer notSatisfyNumFirstMatch = 0; //不满足阈值，但第一条正确的记录。
            List<String> satisfyButErrorList = new ArrayList<>();
            SolrlstmUtils.initialWeightMap(weightPath);
            TokenizeUtils.loCustomDict(customPath);
            TokenizeUtils.loadSynonymDict(synonymPath);
            TokenizeUtils.loadStopwordsDict(stopWodsPath);
            bufferReader.readLine();
            while( (line = bufferReader.readLine()) != null ){
                String[] words = line.split("\t");
                if( words.length != 3  ){
                    System.out.println("error data:"+line);
                    continue;
                }
                index++;
                if( index % 1000 == 0 ){
                    System.out.println("process 1000 line, cost "+((System.currentTimeMillis()-start)*1.0/1000) + "秒");
                    System.out.println("current satisfyThreshold total="+satisfyThresholdTotalNum+", success="+satisfyThresholdSuccessNum
                            +", succeedRate="+(satisfyThresholdSuccessNum.floatValue()/satisfyThresholdTotalNum.floatValue())+
                            ",satisfyThresholdRate "+(satisfyThresholdTotalNum.floatValue()/index));
                    System.out.println("=>notSatisfyNum="+notSatisfyNum+", notSatisfyNumFirstMatch="+notSatisfyNumFirstMatch
                            +", notSatisfyNumFirstMatchRate="+notSatisfyNumFirstMatch.floatValue()/notSatisfyNum+", notSatisfyRate="+notSatisfyNum.floatValue()/index);

                    start = System.currentTimeMillis();
                }
                String usedIntention =SolrlstmUtils.removeLastSlot(words[2]);
                String rawWord = TokenizeUtils.tokenize(words[1]);
                if( StringUtils.isBlank(rawWord) ){
                    System.out.println("blank words:"+line);
                    continue;
                }
                List<WordIntention> topList = SolrlstmUtils.getTopNIntentions(solrClient, rawWord, 5);
                if( topList == null || topList.size() == 0 ){
                    System.out.println("no result found in solr, "+rawWord);
                    continue;
                }
                WordIntention firstMatch = topList.get(0);
                WordIntention secondMatch = SolrlstmUtils.getSecondMatch(topList);
                if( SolrlstmUtils.isSatisfyThreshold(firstMatch,secondMatch) ){
                    satisfyThresholdTotalNum++;
                    if( firstMatch.getIntention().equals(usedIntention) ){
                        satisfyThresholdSuccessNum++;
                    }else {
                        satisfyButErrorList.add(line+";"+firstMatch.getWord()+"\t"+firstMatch.getIntention());
                    }
                }else{
                    //生成模型测试文件
                    notSatisfyNum++;
                    if( firstMatch.getIntention().equals(usedIntention) ){
                        notSatisfyNumFirstMatch++;
                    }
                    String intentions = SolrlstmUtils.getNoSatisfyIntentionSet(topList);
                    modelList.add(rawWord+"\t"+intentions+"\t"+SolrlstmUtils.extractIntentionWord(usedIntention));
                }
            }
            FilesUtils.saveListToFile(modelList, outFile);
            FilesUtils.saveListToFile(satisfyButErrorList, satisfyButErrorFile);
            System.out.println("==>total satisfyThreshold total="+satisfyThresholdTotalNum+", success="+satisfyThresholdSuccessNum+"index="+index
                    +", succeedRate="+(satisfyThresholdSuccessNum.floatValue()/satisfyThresholdTotalNum.floatValue())+
                    ",satisfyThresholdRate "+(satisfyThresholdTotalNum.floatValue()/index));

            System.out.println("===>notSatisfyNum="+notSatisfyNum+", notSatisfyNumFirstMatch="+notSatisfyNumFirstMatch
                    +", notSatisfyNumFirstMatchRate="+notSatisfyNumFirstMatch.floatValue()/notSatisfyNum+", notSatisfyRate="+notSatisfyNum.floatValue()/index);
            System.out.println("==>cost "+((System.currentTimeMillis()-allStart)*1.0/(1000*60)) + "分钟");
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
    }

    public static void main(String[] args) {
        String callMethod = "generateModelTestFile";
        String collectionDirName = "lifeareanavi";
        String collectionFullPath = SolrlstmUtils.BASE_PATH+collectionDirName+"\\";
        String collectionName = "GCC-OMCP_NLPLifeAreaExtensionNavi";
        try{
            if ("generateModelTestFile".equals(callMethod) || "all".equals(callMethod)) {
                String customPath = collectionFullPath+"tokenize_custom_xx.txt";
                String synonymPath = collectionFullPath+"tokenize_synonym_xx.txt";
                String weightPath = collectionFullPath+"tokenize_weight_xx.txt";
                String stopWordsPath = collectionFullPath+"stopwords.txt";
                String testFilePath = collectionFullPath+"raw_test_data.txt";
                String testFile = collectionFullPath+"lifearea_model_test_01.txt";
                String satisfyButErrorFile = collectionFullPath+"satisfy_but_error_01.txt";
                generateModelTestFile(testFilePath,collectionName, customPath,stopWordsPath,synonymPath,weightPath,testFile,satisfyButErrorFile);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("done all!!!");
    }
}
